
label var D_sourcing "\$Relational^D_b\$" 
label var S_sourcing "\$Relational_b\$" 
label var ln_p_inpf_sbjo "\$p^f_{sbjo}\$" 

* CUMULATIVE INTERACTIONS:
capture drop gr_r
egen gr_r=group(s_id b_id)
capture drop xx
gen xx=1
capture drop yy
sort gr_r date
by gr_r: gen yy=sum(xx)  
bys UD_num: egen cumul_int_sb=min(yy)
replace cumul_int_sb=ln(cumul_int_sb)

label var cumul_int_sb "\$Past \ Trade_{sb}\$" 

capture drop xx_bjt
capture drop xx_sbjt
bys gr_bjt: egen xx_bjt=total(q_exp)  
bys gr_sbjt: egen xx_sbjt=total(q_exp)   
 
capture drop xx_sbjt_bjt
capture drop max_sh_sbjt_bjt
 
gen xx_sbjt_bjt=xx_sbjt/xx_bjt

bys gr_bjt: egen max_sh_sbjt_bjt=max(xx_sbjt_bjt)
gen flag1=(max_sh_sbjt_bjt==xx_sbjt_bjt)  // This flags the seller with the largest share in the buyer-product-year  

capture drop sample
gen sample=(n_ud==1 & S_pds==1) 

eststo clear
eststo: reghdfe ln_p_inpf_sbjo  S_sourcing cumul_int_sb c.S_sourcing#c.cumul_int_sb ///
 ln_q_sb_all sh_s_b sh_b_s ln_q_sbjo   ///
 ln_q_b_all age_buy_month  ln_q_inpf signatory ///
if sample==1, ///
abs(gr_sjt DBKUP cohort_sb cohort_b) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjt,d", replace  
  quietly estadd local CTRL2 ="\scriptsize{All}", replace    
  quietly estadd scalar  U =  . , replace 

eststo: reghdfe ln_p_inpf_sbjo   cumul_int_sb c.S_sourcing#c.cumul_int_sb  ///
 ln_q_sb_all sh_s_b sh_b_s ln_q_sbjo   ///
  age_buy_month  ln_q_inpf  ///
if sample==1, ///
abs(gr_sjt gr_r  ) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjt,sb", replace  
  quietly estadd local CTRL2 ="\scriptsize{All}", replace    
  quietly estadd scalar  U =  . , replace    

eststo: reghdfe ln_p_inpf_sbjo   cumul_int_sb c.S_sourcing#c.cumul_int_sb  ///
 ln_q_sb_all sh_s_b sh_b_s ln_q_sbjo   ///
  age_buy_month  ln_q_inpf  ///
if sample==1 & flag1==1, ///
abs(gr_sjt gr_r  ) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjt,sb", replace  
  quietly estadd local CTRL2 ="\scriptsize{Main}", replace    
  quietly estadd scalar  U =  . , replace    

eststo: reghdfe ln_p_inpf_sbjo   cumul_int_sb i1.D_sourcing#c.cumul_int_sb  ///
 ln_q_sb_all sh_s_b sh_b_s ln_q_sbjo   ///
  age_buy_month  ln_q_inpf  ///
if sample==1, ///
abs(gr_sjt gr_r ) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjt,sb", replace  
  quietly estadd local CTRL2 ="\scriptsize{All}", replace    
  quietly estadd scalar  U =  . , replace   
  
esttab using "${tables}\TableE3PanelB.tex", ///
  label replace booktabs b(3) se(3) noconstant obslast substitute(\_ _) ///
  title(" Price of Fabric, Relationship Dynamics, and Fabric Quantities" \label{FabPriceBuyer}) ///
  drop(ln_q_b_all ln_q_sb_all sh_b_s  sh_s_b  age_buy_months  ln_q_sbjo ln_q_inpf signatory _cons) ///
  stats(FE CTRL CTRL2 r2   N   , fmt( %~#s %~#s  %~#s 2 %12.0fc  )  ///
  labels("FEs" "Controls" "Relationships" `"\(R^{2}\)"'  "Obs."  ))   ///
  star(* 0.10 ** 0.05 *** 0.01) ///
  nonotes  	
 
sort  custom_office date registration itm_n
capture drop _merge
merge 1:1  custom_office date registration itm_n using "${source_data}\IV_fabric.dta", keepusing(ln_q_sbq_inst3b f_HS6_1 orig_f_1)
drop _merge

label var ln_q_sbq_inst3b "\$z_{sb\tau}\$" 
 
egen gr_fo=group( f_HS6_1 orig_f_1)  

eststo clear
capture drop sample
gen sample=(n_ud==1 & S_pds==1 & ln_numf==0)
* OLS IN SINGLE FABRIC ORDERS
eststo: reghdfe ln_p_inpf_sbjo    ln_q_inpf  ///
if sample==1 & ln_q_sbq_inst3b!=., ///
abs(gr_sjt  ) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{.}", replace  
  quietly estadd local est ="\scriptsize{Single}", replace  
  quietly estadd local FE ="sjt", replace  
  quietly estadd local CTRL2 ="\scriptsize{OLS}", replace    
  quietly estadd scalar  U =  . , replace  
* FIRST STAGE IN SINGLE FABRIC ORDERS
eststo: reghdfe ln_q_inpf ln_q_sbq_inst3b  ///
if sample==1, ///
abs(gr_sjt) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{44.188}", replace  
  quietly estadd local est ="\scriptsize{Single}", replace  
  quietly estadd local FE ="sjt", replace  
  quietly estadd local CTRL2 ="\scriptsize{First Stage}", replace    
  quietly estadd scalar  U =  . , replace      
* IV IN SINGLE FABRIC ORDERS
eststo: ivreghdfe ln_p_inpf_sbjo    (ln_q_inpf=ln_q_sbq_inst3b)  ///
if sample==1, ///
abs(gr_sjt   ) cluster(b_id)
  quietly estadd local CTRL ="\scriptsize{.}", replace  
  quietly estadd local est ="\scriptsize{Single}", replace  
  quietly estadd local FE ="sjt", replace  
  quietly estadd local CTRL2 ="\scriptsize{IV}", replace    
  quietly estadd scalar  U =  . , replace       
  
capture drop sample
gen sample=(n_ud==1 & S_pds==1)
* OLS IN ALL ORDERS
eststo: reghdfe ln_p_inpf_sbjo    ln_q_inpf  ///
if sample==1 & ln_q_sbq_inst3b!=., ///
abs(gr_sjt  gr_fo ) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{.}", replace  
  quietly estadd local est ="\scriptsize{All}", replace  
  quietly estadd local FE ="sjt,fo", replace  
  quietly estadd local CTRL2 ="\scriptsize{OLS}", replace    
  quietly estadd scalar  U =  . , replace  
* FIRST STAGE IN ALL ORDERS  
eststo: reghdfe ln_q_inpf ln_q_sbq_inst3b  ///
if sample==1, ///
abs(gr_sjt  gr_fo ) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{57.880}", replace  
  quietly estadd local est ="\scriptsize{All}", replace  
  quietly estadd local FE ="sjt,fo", replace  
  quietly estadd local CTRL2 ="\scriptsize{First Stage}", replace    
  quietly estadd scalar  U =  . , replace      
* IV IN ALL ORDERS  
eststo: ivreghdfe ln_p_inpf_sbjo    (ln_q_inpf=ln_q_sbq_inst3b)  ///
if sample==1, ///
abs(gr_sjt  gr_fo ) cluster(b_id)
  quietly estadd local CTRL ="\scriptsize{.}", replace  
  quietly estadd local est ="\scriptsize{All}", replace  
  quietly estadd local FE ="sjt,fo", replace  
  quietly estadd local CTRL2 ="\scriptsize{IV}", replace    
  quietly estadd scalar  U =  . , replace      

esttab using "${tables}\TableE3PanelA.tex", ///
  label replace booktabs b(3) se(3) noconstant obslast substitute(\_ _) ///
  title(" Price of Fabric, Relationship Dynamics, and Fabric Quantities" \label{ExogPriceFab}) ///
  drop( _cons) ///
  stats(FE est CTRL2 CTRL r2   N   , fmt( %~#s %~#s %~#s  %~#s 2 %12.0fc  )  ///
  labels("FEs" "Fabric" "Specification" "KP F-Stat" `"\(R^{2}\)"'  "Obs."  ))   ///
  star(* 0.10 ** 0.05 *** 0.01) ///
  nonotes  	 
 